package cn.buttheway.moodmusicdiary.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import cn.buttheway.moodmusicdiary.models.MoodEntry;
import cn.buttheway.moodmusicdiary.models.Song;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "MoodMusicDiary.db";
    private static final int DATABASE_VERSION = 1;

    private static final String TABLE_MOOD_ENTRIES = "mood_entries";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_DATE = "date";
    private static final String COLUMN_MOOD_TEXT = "mood_text";
    private static final String COLUMN_SENTIMENT = "sentiment";
    private static final String COLUMN_SONG_TITLE = "song_title";
    private static final String COLUMN_SONG_ARTIST = "song_artist";

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTableQuery = "CREATE TABLE " + TABLE_MOOD_ENTRIES + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_DATE + " TEXT, " +
                COLUMN_MOOD_TEXT + " TEXT, " +
                COLUMN_SENTIMENT + " TEXT, " +
                COLUMN_SONG_TITLE + " TEXT, " +
                COLUMN_SONG_ARTIST + " TEXT)";
        db.execSQL(createTableQuery);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MOOD_ENTRIES);
        onCreate(db);
    }

    public long insertMoodEntry(MoodEntry moodEntry) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_DATE, dateFormat.format(moodEntry.getDate()));
        values.put(COLUMN_MOOD_TEXT, moodEntry.getMoodText());
        values.put(COLUMN_SENTIMENT, moodEntry.getSentiment());
        values.put(COLUMN_SONG_TITLE, moodEntry.getSong().getTitle());
        values.put(COLUMN_SONG_ARTIST, moodEntry.getSong().getArtist());
        return db.insert(TABLE_MOOD_ENTRIES, null, values);
    }

    public List<MoodEntry> getRecentMoods(int limit) {
        List<MoodEntry> moodEntries = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "SELECT * FROM " + TABLE_MOOD_ENTRIES + " ORDER BY " + COLUMN_DATE + " DESC LIMIT " + limit;
        Cursor cursor = db.rawQuery(query, null);
        if (cursor.moveToFirst()) {
            do {
                try {
                    Date date = dateFormat.parse(cursor.getString(cursor.getColumnIndex(COLUMN_DATE)));
                    String moodText = cursor.getString(cursor.getColumnIndex(COLUMN_MOOD_TEXT));
                    String sentiment = cursor.getString(cursor.getColumnIndex(COLUMN_SENTIMENT));
                    String songTitle = cursor.getString(cursor.getColumnIndex(COLUMN_SONG_TITLE));
                    String songArtist = cursor.getString(cursor.getColumnIndex(COLUMN_SONG_ARTIST));
                    Song song = new Song(songTitle, songArtist);
                    MoodEntry moodEntry = new MoodEntry(date, moodText, sentiment, song);
                    moodEntries.add(moodEntry);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } while (cursor.moveToNext());
        }
        cursor.close();
        return moodEntries;
    }

    // 在 DatabaseHelper 类中添加以下方法

    public List<MoodEntry> getAllMoodEntries() {
        List<MoodEntry> moodEntries = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "SELECT * FROM " + TABLE_MOOD_ENTRIES + " ORDER BY " + COLUMN_DATE + " DESC";
        Cursor cursor = db.rawQuery(query, null);
        if (cursor.moveToFirst()) {
            do {
                try {
                    Date date = dateFormat.parse(cursor.getString(cursor.getColumnIndex(COLUMN_DATE)));
                    String moodText = cursor.getString(cursor.getColumnIndex(COLUMN_MOOD_TEXT));
                    String sentiment = cursor.getString(cursor.getColumnIndex(COLUMN_SENTIMENT));
                    String songTitle = cursor.getString(cursor.getColumnIndex(COLUMN_SONG_TITLE));
                    String songArtist = cursor.getString(cursor.getColumnIndex(COLUMN_SONG_ARTIST));
                    Song song = new Song(songTitle, songArtist);
                    MoodEntry moodEntry = new MoodEntry(date, moodText, sentiment, song);
                    moodEntries.add(moodEntry);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } while (cursor.moveToNext());
        }
        cursor.close();
        return moodEntries;
    }
}